package com.haoze.nurseapp.service.impl;

import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.common.utils.UUIDUtil;
import com.haoze.nurseapp.dao.WardOverviewLogMapper;
import com.haoze.nurseapp.dto.WardOverviewLogDTO;
import com.haoze.nurseapp.model.WardOverviewLogEntity;
import com.haoze.nurseapp.service.WardOverviewService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WardOverviewServiceImpl implements WardOverviewService {
    @Autowired
    private WardOverviewLogMapper wardOverviewLogMapper;
    public Result query(String wardId,String search,String delFlag){
        String userId = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("zuul_id");
        //判断
        int count = wardOverviewLogMapper.queryWardOverviewLogByWardId(wardId);
        if(count==0){
            //新增
            List<Map<String, String>> basicDataList = wardOverviewLogMapper.queryWardHspBasicDataValue();
            List<WardOverviewLogEntity> wardOverviewLogEntityList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(basicDataList)){
                for(Map<String, String> map:basicDataList){
                    WardOverviewLogEntity wardOverviewLogEntity = new WardOverviewLogEntity(UUIDUtil.randomString(),wardId,map.get("basicdatavalueId"),"1",userId);
                    wardOverviewLogEntity.initAdd();
                    wardOverviewLogEntityList.add(wardOverviewLogEntity);
                }
                //新增
                wardOverviewLogMapper.insertWardOverviewLog(wardOverviewLogEntityList);
            }
        }
        //查询
        List<WardOverviewLogDTO> wardOverviewLogDTOS = wardOverviewLogMapper.queryWardOverviewLogListByWardId(wardId, search, delFlag);
        return ResultGenerator.genOkResult(wardOverviewLogDTOS);

    }

    public Result updateStatus(String delFlag,String ids){
        String userId = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("zuul_id");
        if(StringUtils.isNotBlank(ids)){
            String[] idstr = ids.split(",");
            wardOverviewLogMapper.updateStatus(idstr,delFlag,userId);
            return ResultGenerator.genOkResult("设置成功!");
        }else{
            return ResultGenerator.genFailedResult("未选择数据!");
        }

    }
}
